package com.lagou.order.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.common.feign.GoodsFeign;
import com.lagou.common.pojo.OrderVo;
import com.lagou.common.pojo.Orders;
import com.lagou.common.pojo.ResponseResult;
import com.lagou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @BelongProject: lagou-parent
 * @author: 胡恺恺
 * @createTime: 2021/1/29  21:36
 * @Description:
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //远程调用
    @Autowired
    private GoodsFeign goodsFeign;

     /*
        用户分页&多条件组合查询方法
     */

    @GetMapping("/queryByPage")
    public ResponseResult queryByPage(OrderVo orderVo){

        PageInfo pageInfo = orderService.queryByPage(orderVo);

        //封装响应数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("list", pageInfo.getList());

        return new ResponseResult(true,200,"分页多条件查询成功",map);

    }

    /**
     * 通过id查询订单
     */
    @GetMapping("/queryById/{id}")
    public ResponseResult queryById(@PathVariable("id") Integer id){

        Orders orders = orderService.queryById(id);

        return new ResponseResult(true,200,"查询订单成功",orders);

    }

    /**
     * 通过id查询商品
     */
    @GetMapping("/findGoodsByOrderId/{id}")
    public ResponseResult findGoodsByOrderId(@PathVariable("id") Integer id){

        List<Integer> goodsIdList = orderService.findGoodsIdByOrderId(id);

        System.out.println(goodsIdList);

        ArrayList<Object> list = new ArrayList<>();

        for (Integer goodsId : goodsIdList) {
            ResponseResult responseResult = goodsFeign.queryById(goodsId);
            list.add(responseResult.getContent());
        }

        return new ResponseResult(true,200,"通过订单id查询商品列表成功",list);
    }

}
